import { createStore } from 'redux'
// createStore(function, 初始数据, 添加中间件)   返回值是 {}
// 初次使用reducer会执行
// 调用dispatch的时候会执行reducer函数

let reducer = (state, action) => {
    // console.log('reducer函数执行了', action)
    // if (action.type === "ADD") {
    //     state.count += 1
    // } else if (action.type === "DEL") {
    //     state.count -= 1
    // }
    switch(action.type) {
        case "ADD":
            state.count += 1
            return state
        case "INIT":
            axios.get('/url').then(res => {
                state.num = res.data
            })
            return state
        default:
            return state
    }
}

let initState = {
    count: 100,
    num: 888
}

let store = createStore(reducer, initState)

// store.getState() 获取redux仓库中的值   读取的内容和reducer函数的返回值有关

// store.subscribe(() => {})  redux中的数据修改后，会执行内部的函数  

// console.log(store.getState())
window.store = store

export default store